package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.common.Contants;
import com.qeeyou.apps.android.craftsman.po.AreaInfo;
import com.qeeyou.apps.android.craftsman.po.CommonInfo;
import com.qeeyou.apps.android.craftsman.po.UserAddressInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.service.UserService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddAddressActivity extends Activity {

    private ImageView btnBack;
    private Button btnConfirm;                  //确定按钮
    private EditText etName;
    private LinearLayout llProvinceCity;
    private TextView tvProvinceCity;
    private EditText etAddress;
    private EditText etPhone;

    private CustomLoadingDialog mLoadingDialog;

    private UserAddressInfo mUserAddressInfo;

    private List<AreaInfo> mAreaList = new ArrayList<AreaInfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);

        mUserAddressInfo = getIntent().getParcelableExtra(Contants.Extra.EXTRA_ADDRESS_INFO);

        setView();
        setEvent();
    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4AddAddress);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4AddAddress);
        etName = (EditText) this.findViewById(R.id.etName4AddAddress);
        llProvinceCity = (LinearLayout) this.findViewById(R.id.llProvinceCity4AddAddress);
        tvProvinceCity = (TextView) this.findViewById(R.id.tvProvinceCity4AddAddress);
        etAddress = (EditText) this.findViewById(R.id.etAddress4AddAddress);
        etPhone = (EditText) this.findViewById(R.id.etPhone4AddAddress);

        if (mUserAddressInfo != null) {
            etName.setText(mUserAddressInfo.getName());
            tvProvinceCity.setText(mUserAddressInfo.getProvinceid() + mUserAddressInfo.getCityid());
            etAddress.setText(mUserAddressInfo.getAddress());
            etPhone.setText(mUserAddressInfo.getContactway());
        }
    }

    private void setEvent() {
        btnBack.setOnClickListener(onBackClickListener);
        btnConfirm.setOnClickListener(onConfirmClickListener);

        llProvinceCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(AddAddressActivity.this, AddressSelectorActivity.class)
                        , Contants.RequestCode.REQUEST_SELECT_PROVINCE_CITY);
            }
        });

        //获取省市区选择信息 TODO 选择框还未实现
        new GetAllAreaList().execute();
    }

    //返回按钮
    private View.OnClickListener onBackClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddAddressActivity.this.finish();
        }
    };

    //确定按钮
    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = etName.getText().toString();
            String provinceCity = tvProvinceCity.getText().toString();
            String address = etAddress.getText().toString();
            String phone = etPhone.getText().toString();
            if (TextUtils.isEmpty(name)) {
                Toast.makeText(AddAddressActivity.this, "请输入您的姓名", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(provinceCity)) {
                Toast.makeText(AddAddressActivity.this, "请选择您的省市区", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(address)) {
                Toast.makeText(AddAddressActivity.this, "请填写详细的楼号门牌号", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(AddAddressActivity.this, "请输入你的电话号码", Toast.LENGTH_SHORT).show();
                return;
            }

            String[] params = {name, "2", "2830", address, "121.351303,31.387385", phone, "1"};
            new ModifyUserAddress().execute(params);
        }
    };

    class GetAllAreaList extends AsyncTask<String, Void, List<AreaInfo>> {

        public GetAllAreaList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected List<AreaInfo> doInBackground(String... params) {
            List<AreaInfo> areaList = AppService.getInstance().getAllArea();
            return areaList;
        }

        @Override
        protected void onPostExecute(List<AreaInfo> areaList) {
            super.onPostExecute(areaList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (areaList != null && areaList.size() > 0) {
                mAreaList = areaList;
            }
        }
    }

    //保存用户地址
    class ModifyUserAddress extends AsyncTask<String, Void, CommonInfo> {

        public ModifyUserAddress() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(AddAddressActivity.this);
            }
            mLoadingDialog.setMessage("正在提交用户地址...");
            mLoadingDialog.show();
        }

        @Override
        protected CommonInfo doInBackground(String... params) {
            String name = params[0];
            String provinceid = params[1];
            String cityid = params[2];
            String address = params[3];
            String coordinates = params[4];
            String contactway = params[5];
            String isdefault = params[6];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("name", name);
            paramMap.put("provinceid", provinceid);
            paramMap.put("cityid", cityid);
            paramMap.put("address", address);
            paramMap.put("coordinates", coordinates);
            paramMap.put("contactway", contactway);
            paramMap.put("isdefault", isdefault);
            CommonInfo resultInfo = UserService.getInstance().modifyUserAddress(paramMap);
            return resultInfo;
        }

        @Override
        protected void onPostExecute(CommonInfo resultInfo) {
            super.onPostExecute(resultInfo);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (resultInfo != null && resultInfo.getStatus().equalsIgnoreCase("1")) {
                Toast.makeText(AddAddressActivity.this, "提交用户地址成功", Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        AddAddressActivity.this.finish();
                    }
                }, 1000);
            } else {
                Toast.makeText(AddAddressActivity.this, "提交用户地址失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Contants.RequestCode.REQUEST_SELECT_PROVINCE_CITY:
                if (resultCode == RESULT_OK) {
                    String provinceCityStr = data.getStringExtra("address");
                    if(provinceCityStr != null) {
                        provinceCityStr = provinceCityStr.replace("-", "");
                    }
                    tvProvinceCity.setText(provinceCityStr);
                }
                break;
        }
    }
}
